<?php

namespace app\admin\controller;
use app\data\model\ShopGoodsService;
use think\admin\Controller;
/**
 * 商品服务管理
 */
class Service  extends Controller
{
/**
     * 商品服务列表
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = '商品服务管理';
        // 订单列表查询
        ShopGoodsService::mQuery()->where(['status'=>1])->order('service_id desc')->page();
    }
    /**
     * 编辑商品服务列表
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit(){
        $this->_applyFormToken();
        $id = input('id','','trim,intval');
        if ($this->request->isGet()) {
            $this->title = '编辑商品服务列表';
            $service = [];
            if(empty($id) === FALSE){
                $service = ShopGoodsService::mk()->where(['service_id'=>$id])->find()->toArray();
            }
            $this->service = $service;
            $this->fetch();
        } else {
            $post = $this->request->post();
            if(!isset($post['service_id']) || empty($post['service_id'])){
                $res = ShopGoodsService::mk()->insert(array(
                    'name'  => $post['name'],
                    'summary'  => $post['summary'],
                    'create_at'  => \Tools::getDateTimeByTime(),
                    'update_at'  => \Tools::getDateTimeByTime(),
                ));
            }else{
                $res = ShopGoodsService::mk()->where(['service_id'=>$post['service_id']])->save(array(
                    'name'  => $post['name'],
                    'summary'  => $post['summary'],
                    'update_at'  => \Tools::getDateTimeByTime(),
                ));
            }
            if($res) {
                return \Tools::returnMsg(true,$res);
            }else{
                return \Tools::returnMsg(false,array(
                    'code'  =>'100019',
                    'message'=>'编辑商品服务失败'
                ));
            }
        }
    }
    /**
     * 删除商品服务列表
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete()
    {
        $id = input('id','','trim,intval');
        if(empty($id)) $this->error('参数错误');
        $res = ShopGoodsService::mk()->where(['service_id'=>$id])->delete();
        if($res) {
            return \Tools::returnMsg(true,$res);
        }else{
            return \Tools::returnMsg(false,array(
                'code'  =>'100019',
                'message'=>'删除商品服务失败'
            ));
        }
    }
}
